<?php

require_once '../include/common.inc.php';

if (!checkAdmin()) {
    header("LOCATION:index.php");
    exit;
}

$menu_index = 6;
$page_title = 'JANSEN';
$guide_message = 'User | Profile | Add';
$alert_message = '';

getMyParam('action');
getMyParam('name');
//getMyParam('truename');
getMyParam('pwd');
getMyParam('retype_pwd');
getMyParam('email');
getMyParam('phone');
getMyParam('fax');

$user_type = 10;

$name = ($name == 'Enter name ...') ? '' : $name;

if ($action == "add") {
    $sql = "SELECT `id`, `username`, `email` FROM `user` WHERE `status` <>3 AND (`username` = '$name' OR `email` = '$email')";
    $rs = $db->get_one($sql);
    if (!empty($rs)) {
        if ($rs['username'] == $name) {
            $alert_message = 'Name exist';
        } else {
            $alert_message = 'Email exist';
        }
    }
    if (strlen($name) <2 || strlen($name) > 32) {
        $alert_message = 'Your name must be no more than 32 characters.';
    }
    /*
    if (strlen($truename) > 60) {
        $alert_message = 'Your True name must be no more than 60 characters.';
    }*/
    if (trim($pwd) == "" || trim($retype_pwd) == "") {
        $alert_message = "Please enter password.";
    } elseif (strlen($pwd) > 32) {
        $alert_message = "Your password must be no more than 32 characters.";
    } elseif ($pwd != $retype_pwd) {
        $alert_message = "The passwords you entered did not match.";
    }
    if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/i", $email)) {
        $alert_message = "Invalid Email.";
    }
    if (strlen($phone) > 60) {
        $alert_message = 'Your phone name must be no more than 60 characters.';
    }
    if (strlen($fax) > 60) {
        $alert_message = 'Your fax name must be no more than 60 characters.';
    }

    if ($alert_message == "") {
        $sql = "INSERT INTO `user` (`username`, `password`, `phone`, `fax`, `email`, `type`) VALUES ('$name', '" . md5($pwd) . "', '$phone', '$fax', '$email', '10')";
        $db->query($sql);
        header("LOCATION:user.php");
    }

}

include_once(JANSEN_ROOT . 'admin/menu_list.php');
require_once PrintEot('a_header');
require_once PrintEot('a_user_add');
require_once PrintEot('a_footer');

?>